<template>
    <div>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="选择类型" prop="id"  reuqired>
                <el-select v-model="form.id">
                    <el-option
                        v-for="item in parentOptions"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="名字" prop="name" reuqired >
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="英文值" prop="key" reuqired>
                <el-input v-model="form.key"></el-input>
            </el-form-item>
            <el-form-item label="排序"
                :prop="'order'"
                :rules="[
                    {
                        required: true, message: '排序不能为空', trigger: 'blur'
                    },
                    {
                        pattern:  /^\d+$/, message: '请输入有效的排序(数值)' 
                    }
                ]">
                <el-input v-model="form.order"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit('form')">立即创建</el-button>
                <el-button type="primary" @click="onCancel()">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {computedPostSmDictionaryAdd} from "../.././css_js_img/computedSmDictionaryController"
    
    export default {
        data(){
            return {
                form : {
                    id: "",
                    name: "",
                    order: 1,   //默认排序
                },
                rules:{
                    id: [
                        { required: true, message: '请选择字典类型', trigger: 'change' },
                    ],
                    name:[
                        {required:true,message:'请输入名字',trigger:'blur'}
                    ],
                    key:[
                        {required:true,message:'请输入英文值',trigger:'blur'}
                    ],
                },
            }
        },
        props: [
            "parentOptions",
        ],
        methods: {
            onSubmit(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        computedPostSmDictionaryAdd(this,this.form);
                    } else {
                        this.$message({
                            showClose: true,
                            message: '请正确填完必要信息',
                            type: 'error'
                        });
                        return false;
                    }
                });
            },
            onCancel(){
                this.$emit('cancel',false);
            },
            refresh(){
                this.$emit("listSearch");
            }
        },
    }
</script>